﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T01
{
    class UserDao
    {
        //MySQL的连接串描述
        private static string connStr
            = "server=localhost;user=root;password=;database=gametest;";

        public static int LoginCheck(string username, string password)
        {
            //默认为无效的用户名和密码
            int number = 0;
            //构建MySQL连接对象
            MySqlConnection conn = new MySqlConnection(connStr);

            try {
           
            //打开数据库连接
            conn.Open();
            //比较用户名与密码的SQL语句
            string sql = "SELECT COUNT(id) FROM `tb_users` WHERE `username`='"
                + username + "' AND `password`='" + password + "'";
                string message = "SQL语句：" + sql;
                Debug.WriteLine(message);
            //创建SQL命令
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            //执行SQL命令
            object result = cmd.ExecuteScalar();
            if (result != null)
            {
                number = Convert.ToInt32(result);
            }
        }
            catch (Exception ex)
            {
                Debug.WriteLine("数据库访问异常");
                Debug.WriteLine("=================");
                Debug.WriteLine(ex.ToString());
                Debug.WriteLine("=================");
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
            return number;
        }
        }
}
